package com.yixia.web.controller.system;

import com.yixia.system.domain.system.SysPost;
import com.yixia.system.service.system.SysPostService;
import com.yixia.common.enums.BusinessType;
import com.yixia.common.core.controller.BaseController;
import com.yixia.common.core.domain.AjaxResult;
import com.yixia.common.annotation.Log;

import com.yixia.common.utils.page.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 依梦
 * @Date 2023/8/6
 */
@Slf4j
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {

    @Autowired
    private SysPostService postService;

    /**
     * 进行分页查询
     */
    @GetMapping("/list")
    @PreAuthorize("@yixia.hasPermi('system:post:list')")
    public AjaxResult list(SysPost post){
        PageBean pageBean = startPage();
        return postService.selectPostPageList(pageBean, post);
    }

    /**
     * 添加岗位
     */
    @PostMapping("/add")
    @Log(title = "岗位管理---添加岗位", businessType = BusinessType.INSERT)
    @PreAuthorize("@yixia.hasPermi('system:post:add')")
    public AjaxResult add(@RequestBody SysPost post){
        /*1. 先判断postName是否重复*/
        if (!postService.checkPostNameUnique(post))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        /*2. 判断postCode是否唯一*/
        else if (!postService.checkPostCodeUnique(post))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        /*3. 进行添加*/
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @PutMapping("/edit")
    @Log(title = "岗位管理---修改岗位", businessType = BusinessType.UPDATE)
    @PreAuthorize("@yixia.hasPermi('system:post:edit')")
    public AjaxResult edit(@RequestBody SysPost post){
        /*1. 先判断postName是否重复*/
        if (!postService.checkPostNameUnique(post))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        /*2. 判断postCode是否唯一*/
        else if (!postService.checkPostCodeUnique(post))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        /*3. 进行添加*/
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @DeleteMapping("/delete/{postIds}")
    @Log(title = "岗位管理---删除岗位", businessType = BusinessType.DELETE)
    @PreAuthorize("@yixia.hasPermi('system:post:remove')")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(postService.deletePostByIds(postIds));
    }

}
